package
{
	import com.adobe.viewsource.ViewSource;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getQualifiedClassName;
	
	import mx.containers.HBox;
	import mx.containers.ViewStack;
	import mx.controls.Alert;
	import mx.core.IVisualElement;
	import mx.events.ChildExistenceChangedEvent;
	import mx.events.FlexEvent;
	import mx.states.OverrideBase;
	
	import spark.components.HGroup;
	import spark.components.Image;
	import spark.components.Label;
	import spark.components.NumericStepper;
	import spark.components.Panel;

	//--------------------------------------
	//  Events
	//--------------------------------------
	[Event(name="imageBlackLeftClick", type="PagingPanelEvent")]
	[Event(name="imageBlackRightClick", type="PagingPanelEvent")]
	[Event(name="imageWhiteLeftClick", type="PagingPanelEvent")]
	[Event(name="imageWhiteRightClick", type="PagingPanelEvent")]
	[Event(name="addPage", type="PagingPanelEvent")]
	[Event(name="deletePage", type="PagingPanelEvent")]
	
	public class PagingPanel extends Panel
	{

		[SkinPart(required="false")]
		public var imgBlackLeft:Image;
		[SkinPart(required="false")]
		public var imgBlackRight:Image;
		[SkinPart(required="false")]
		public var imgWhiteLeft:Image;
		[SkinPart(required="false")]
		public var imgWhiteRight:Image;
		
		[SkinPart(required="false")]
		public var labelAdd:Label;
		[SkinPart(required="false")]
		public var labelDel:Label;
		
		/*[SkinPart(required="false")]
		public var pageNumberGroup:HBox;	*/	
		[SkinPart(required="false")]
		public var pageNumberGroup:HGroup;
		[SkinPart(required="false")]
		public var pageViewStack:ViewStack;	
		/*[SkinPart(required="false")]
		public var pageNumViewStack:ViewStack;*/	
		[SkinPart(required="false")]
		public var pageNumViewStack:CBViewStack;	
		/*[SkinPart(required="false")]
		public var pageNumericGroup:HBox;	*/
		[SkinPart(required="false")]
		public var pageNumericGroup:HGroup;	
		[SkinPart(required="false")]
		public var numStep:NumericStepper;	
		[SkinPart(required="false")]
		public var numberOfPage:Label;

		override protected function updateDisplayList(width:Number,height:Number):void 
		{ 
			super.updateDisplayList(width,height); 
			if (pageNumberGroup)
			{
				trace('updateDisplayList',pageNumberGroup.width); 
				trace('updateDisplayList',pageNumberGroup.measuredWidth); 
				trace('updateDisplayList',pageNumberGroup.measuredMinWidth); 			
				trace(pageNumberGroup.getExplicitOrMeasuredWidth());
				trace(this.width - imgBlackLeft.width - 
					imgBlackRight.width - imgWhiteLeft.width - imgWhiteRight.width - 
					labelAdd.width - labelDel.width);
				pageNumViewStack.width;
				
				if (pageNumberGroup.measuredWidth > (this.width - imgBlackLeft.width - 
					imgBlackRight.width - imgWhiteLeft.width - imgWhiteRight.width - 
					labelAdd.width - labelDel.width - 60))
				/*if (pageNumberGroup.measuredWidth > pageNumViewStack.width)*/
				{
					pageNumViewStack.selectedIndex=1;
				}
				else
				{	
					pageNumViewStack.selectedIndex=0;			
				}
			}
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if (instance == imgBlackLeft)
			{
				imgBlackLeft.addEventListener(MouseEvent.CLICK, imgBlackLeft_clickHandler);
			}
			else if (instance == imgBlackRight)
			{
				imgBlackRight.addEventListener(MouseEvent.CLICK, imgBlackRight_clickHandler);   
			}	
			else if (instance == imgWhiteLeft)
			{
				imgWhiteLeft.addEventListener(MouseEvent.CLICK, imgWhiteLeft_clickHandler);   
			}
			else if (instance == imgWhiteRight)
			{
				imgWhiteRight.addEventListener(MouseEvent.CLICK, imgWhiteRight_clickHandler);   
			}
			else if (instance == labelAdd)
			{
				labelAdd.addEventListener(MouseEvent.CLICK, labelAdd_clickHandler);   
			}
			else if (instance == labelDel)
			{
				labelDel.addEventListener(MouseEvent.CLICK, labelDel_clickHandler);   
			}
			else if (instance == pageViewStack)
			{
				pageViewStack.addEventListener(ChildExistenceChangedEvent.CHILD_ADD, pageViewStack_addHandler);   
				pageViewStack.addEventListener(ChildExistenceChangedEvent.CHILD_REMOVE, pageViewStack_removeHandler);   
			}
			else if (instance == numStep)
			{
				numStep.addEventListener(flash.events.Event.CHANGE, numStep_changeHandler);
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			
			if (instance == imgBlackLeft)
			{
				imgBlackLeft.removeEventListener(MouseEvent.CLICK, imgBlackLeft_clickHandler);
			}
			else if (instance == imgBlackRight)
			{
				imgBlackRight.removeEventListener(MouseEvent.CLICK, imgBlackRight_clickHandler);   
			}	
			else if (instance == imgWhiteLeft)
			{
				imgWhiteLeft.removeEventListener(MouseEvent.CLICK, imgWhiteLeft_clickHandler);   
			}
			else if (instance == imgWhiteRight)
			{
				imgWhiteRight.removeEventListener(MouseEvent.CLICK, imgWhiteRight_clickHandler);   
			}	
			else if (instance == labelAdd)
			{
				labelAdd.removeEventListener(MouseEvent.CLICK, labelAdd_clickHandler);   
			}
			else if (instance == labelDel)
			{
				labelDel.removeEventListener(MouseEvent.CLICK, labelDel_clickHandler);   
			}
			else if (instance == pageViewStack)
			{
				pageViewStack.removeEventListener(ChildExistenceChangedEvent.CHILD_ADD, pageViewStack_addHandler);
				pageViewStack.removeEventListener(ChildExistenceChangedEvent.CHILD_REMOVE, pageViewStack_removeHandler);
			}
			else if (instance == numStep)
			{
				numStep.removeEventListener(flash.events.Event.CHANGE, numStep_changeHandler);
			}
		}

		protected function pageViewStack_addHandler(event:ChildExistenceChangedEvent):void
		{
			if (pageViewStack.length>0 && pageNumberGroup)
			{
				var label:Label = new Label();
				label.text = String(pageViewStack.length);
				label.buttonMode = true;
				label.addEventListener(MouseEvent.CLICK, number_clickHandler)
				trace(label.width);
				pageNumberGroup.addElement(label);
				
				var devider:Label = new Label();
				devider.text = "|";
				trace(devider.width);
				pageNumberGroup.addElement(devider);
				trace(pageNumberGroup.measuredWidth);
				
				numStep.minimum = 1;
				numStep.maximum = pageViewStack.length;
				numberOfPage.text = String(pageViewStack.length);
				numStep.value = pageViewStack.length;
				if (numStep.textDisplay)
					numStep.textDisplay.text = String(pageViewStack.length);
			}
		}

		protected function pageViewStack_removeHandler(event:ChildExistenceChangedEvent):void
		{
			if (pageViewStack.length>0)
			{
				pageNumberGroup.removeElementAt(pageNumberGroup.numElements - 1);
				pageNumberGroup.removeElementAt(pageNumberGroup.numElements - 1);
				
				numStep.minimum = 1;
				numStep.maximum = pageViewStack.length - 1;
				numberOfPage.text = String(pageViewStack.length - 1);
				
				if (numStep.textDisplay)
					numStep.textDisplay.text = String(pageViewStack.length - 1);				
			}
		}
		
		protected function number_clickHandler(event:MouseEvent):void
		{
			boldPageNumber(Number((event.currentTarget as Label).text));
		}

		
		protected function numStep_changeHandler(event:Event):void
		{
			pageViewStack.selectedIndex = numStep.value - 1;
		}
		
		protected function imgBlackLeft_clickHandler(event:MouseEvent):void
		{
			pageViewStack.selectedIndex = 0;
			boldPageNumber(pageViewStack.selectedIndex + 1);
			numStep.value = pageViewStack.selectedIndex + 1;
			dispatchEvent(new PagingPanelEvent(PagingPanelEvent.IMAGE_BLACK_LEFT_CLICK));
		}
		
		protected function imgBlackRight_clickHandler(event:MouseEvent):void
		{
			pageViewStack.selectedIndex = pageViewStack.length - 1;
			boldPageNumber(pageViewStack.selectedIndex + 1);
			numStep.value = pageViewStack.selectedIndex + 1;
			dispatchEvent(new PagingPanelEvent(PagingPanelEvent.IMAGE_BLACK_RIGHT_CLICK));
		}
		
		protected function imgWhiteLeft_clickHandler(event:MouseEvent):void
		{
			if (pageViewStack.selectedIndex != 0)
			{
				pageViewStack.selectedIndex -= 1;
				numStep.value -= 1;
			}
			boldPageNumber(pageViewStack.selectedIndex + 1);
			dispatchEvent(new PagingPanelEvent(PagingPanelEvent.IMAGE_WHITE_LEFT_CLICK));
		}
		
		protected function imgWhiteRight_clickHandler(event:MouseEvent):void
		{
			if (pageViewStack.selectedIndex != (pageViewStack.length - 1))
			{
				pageViewStack.selectedIndex += 1;
				numStep.value += 1;
			}
			boldPageNumber(pageViewStack.selectedIndex + 1);
			dispatchEvent(new PagingPanelEvent(PagingPanelEvent.IMAGE_WHITE_RIGHT_CLICK));
		}
		
		protected function labelAdd_clickHandler(event:MouseEvent):void
		{	
			dispatchEvent(new PagingPanelEvent(PagingPanelEvent.ADD_PAGE));
		}
		
		protected function labelDel_clickHandler(event:MouseEvent):void
		{
			if (pageViewStack.numElements > 0)
			{
				if (pageViewStack.selectedIndex == (pageViewStack.numElements - 1))
				{
					pageViewStack.removeElementAt(pageViewStack.selectedIndex);
					boldPageNumber(pageViewStack.selectedIndex + 1);
				}
				else
				{
					pageViewStack.removeElementAt(pageViewStack.selectedIndex);
				}
			}
			dispatchEvent(new PagingPanelEvent(PagingPanelEvent.DELETE_PAGE));
		}
		
		public function addPage(iv:IVisualElement):void
		{
			pageViewStack.addElement(iv);
			pageViewStack.selectedIndex = pageViewStack.length - 1;
			boldPageNumber(pageViewStack.selectedIndex + 1);

		}
		
		public function boldPageNumber(n:Number):void
		{
			if (pageNumberGroup)
			{
				for (var i:int=0; i<pageNumberGroup.numElements; i++)
				{
					(pageNumberGroup.getElementAt(i) as Label).setStyle("fontWeight", "normal");
				}
	
				for (var j:int = 0; j<pageNumberGroup.numElements; j++)
				{
					var label:Label = pageNumberGroup.getElementAt(j) as Label;				
					if (label.text == String(n))
					{
						pageViewStack.selectedIndex = n - 1;
						label.setStyle("fontWeight", "bold");
						break;
					}
				}
			}
		}
		
		public function PagingPanel()
		{
			super();
		}
	}
}